package org.cicd.devops

import org.cicd.enums.BuildType
import org.cicd.enums.EnvInfoType
import org.cicd.modle.AppInfoVars
import org.cicd.utils.ShellLine
import org.cicd.utils.utils

/**
 * 打包
 */
def packaging(AppInfoVars appInfoVars) {
    String[] envNames = appInfoVars.getDeployEnvNames();
    for(envName in envNames) {
        switch (appInfoVars.buildType) {
            case BuildType.MVN:
                packagingGitMvnTemplate(envName, appInfoVars)
                break;
            case BuildType.NPM:
                packagingGitNpmTemplate(envName, appInfoVars)
                break;
            default:
                break;
        }
    }
}

/**
 *
 * @param envName
 */
def packagingGitNpmTemplate(envName, AppInfoVars appInfoVars) {

}

/**
 * 执行
 * @param envName
 */
def packagingGitMvnTemplate(envName,AppInfoVars appInfoVars) {
    // 清理包
    clearPackage();

    // clone 包仓库
    clonePackageRep(envName)

    // cp 包到 仓库
    cpPackage()

    // 推送包
    pushPackage(envName, appInfoVars)

    // 清理包
    clearPackage()
}

/**
 * git clone 打包制品仓库
 */
def clonePackageRep( envName=EnvInfoType.dev) {
    def  gitServer = new gitServer();
    def util = new utils();
    def repUrl = util.getZipPackageGitReposity(envName)
    util.printInfoMessage("获取打包仓库地址:"+ repUrl)
    dir("${workspace}"+util.getFileSeparator()+"target") {
       gitServer.gitClone("http://192.168.1.137/ptjg/package.git", envName)
    }

}


def cpPackage() {
    def util = new utils();
    util.printInfoMessage("打印当前拷贝地址: " + pwd())
    dir("${workspace}"+util.getFileSeparator()+"target"){
        util.cp("*.zip","."+util.getFileSeparator()+"package")
    }
}

def pushPackage(envName=EnvInfoType.dev, AppInfoVars appInfoVars) {
    def util = new utils();
    getBuild
    def  repPath = "${workspace}"+util.getFileSeparator()+"target"+util.getFileSeparator()+"package"
    util.printInfoMessage("推送文件 ")
    dir(repPath) {
        util.exeShell "git add -A "
        util.exeShell "git commit -m ‘${appInfoVars.BUILD_USER} 打包文件’"
        util.exeShell "git push origin ${envName} "
    }
}


/**
 * 清理包
 */
def clearPackage() {
    def util = new utils();
    def shellLine = new ShellLine();
    util.printInfoMessage("清理包 target 包")
    dir("${workspace}") {
        util.exeShell shellLine.delFolder("target"+util.getFileSeparator()+"package")
    }
}

